/**
 * @file 	CFGBuilder.java
 * 
 * @date 	2/23/2011
 * 
 * @author 	Aaron Alaniz (aaron.a.alaniz@gmail.com)
 * 
 * @brief	This class provides an implementation for building a 
 * 			control flow graph for the "allNodesReachable" method
 * 			in the "ControlFlowGraph" class. 
 *
 */

public class CFGBuilder {
	
	static String testClass;
	static String testMethod;
	
	static ControlFlowGraph build() {
		ControlFlowGraph cfg = new ControlFlowGraph();
		
		// Add appropriate nodes
		cfg.addNode(testClass, testMethod, 1); // Node iterator declaration
		cfg.addNode(testClass, testMethod, 2); // Start while
		cfg.addNode(testClass, testMethod, 3); // Retrieve next node
		cfg.addNode(testClass, testMethod, 4); // if statement
		cfg.addNode(testClass, testMethod, 5); // return false
		cfg.addNode(testClass, testMethod, 6); // end if (unreachable node)
		cfg.addNode(testClass, testMethod, 7); // end while
		cfg.addNode(testClass, testMethod, 8); // return true
		
		// Add appropriate edges
		cfg.addEdge(testClass, testMethod, 1, 2); // iterator to while
		cfg.addEdge(testClass, testMethod, 2, 3); // itr has next
		cfg.addEdge(testClass, testMethod, 2, 8); // itr does not have next
		cfg.addEdge(testClass, testMethod, 3, 4); // n to if
		cfg.addEdge(testClass, testMethod, 4, 5); // not reachable
		cfg.addEdge(testClass, testMethod, 4, 7); // reachable (endwhile)
		cfg.addEdge(testClass, testMethod, 7, 2); // back to while conditional
		
		return cfg;
	}
	
	public static void main(String a[]) {
		testClass = "ControlFlowGraph";
		testMethod = "allNodesReachable";
		
		ControlFlowGraph cfgAllNodesReachable = build();
		System.out.println(cfgAllNodesReachable.toString());
	}
}